home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / MacApp Release 10 / MacApp Release 10 - HD Ready / Examples / Calc / UCalcCells.h < prev    next >
Encoding:
Text File  |  1996-04-03  |  10.4 KB  |  413 lines  |  [TEXT/MPS ]

  1. // UCalcCell.h
  2. // Copyright © 1986-96 by Apple Computer, Inc. All rights reserved. 
  3.  
  4. #ifndef __UCALCCELL__
  5. #define __UCALCCELL__
  6.  
  7. //
  8. // INTERNAL INCLUDES FOR CALC
  9. //
  10.  
  11. #ifndef __UCALC__
  12. #include "UCalc.h"
  13. #endif
  14.  
  15. #ifndef __CALCUTILITIES__
  16. #include "CalcUtilities.h"
  17. #endif
  18.  
  19.  
  20. class TCell;
  21. class TCellList;
  22. class CCalcCellIterator;
  23. class CSelectedExistingCalcCellIterator;
  24.  
  25. //--------------------------------------------------------------------------------------------------
  26. // TCellList:
  27. //--------------------------------------------------------------------------------------------------
  28.  
  29. class TCellList : public TList
  30. {
  31.     MA_DECLARE_CLASS;
  32.     
  33. public:
  34.     virtual ~TCellList();
  35.         // Destructor
  36.     
  37.     virtual void ICellList();
  38.     // Init the list procedurally 
  39.  
  40.     virtual CompareResult Compare(TObject* item1, TObject* item2);    // override 
  41.     // Compares cells based on row and column number 
  42.  
  43.     virtual TCell* GetCell(RowNumber r, ColumnNumber c);
  44.     // Returns the cell whose row,column is r,c 
  45. };    // TCellList 
  46.  
  47.  
  48. //--------------------------------------------------------------------------------------------------
  49. // TCell: TCell describes one element of the spreadsheet grid. 
  50. //--------------------------------------------------------------------------------------------------
  51.  
  52. class TCell : public TObject
  53. {
  54.     MA_DECLARE_CLASS;
  55.     
  56. public:
  57.     Boolean            fDeleted;                    // Whether the cell has been deleted 
  58.     TCalcDocument*    fCalcDocument;                // The document I belong to 
  59.  
  60.     RowNumber        fRow;                        // Which row I'm in 
  61.     ColumnNumber    fColumn;                    // Which column I'm in 
  62.  
  63.     KindOfCell        fKind;                        // What kind of cell: text, value, empty, error
  64.     EvalResult        fError;                        // Error code from my latest evaluation 
  65.     Boolean            fEvaluating;                // flag for preventing self reference loops
  66.     ValueType        fValue;                        // My value represented as a number 
  67.     ValueString        fValueString;                // My value represented as a CString, for display
  68.     CStr255            fFormula;                    // My contents as typed in by the user 
  69.     Boolean            fMarked;                    // !!!RCR For change notification 
  70.  
  71.     TCell();        
  72.     // Constructor
  73.  
  74.     virtual void ICell(TCalcDocument* owningDocument,
  75.                        RowNumber r,
  76.                        ColumnNumber c);
  77.     // Initialize the cell's fields 
  78.  
  79.     virtual TObject* Clone();        // override 
  80.     /* Create a new cell which is a copy of this cell, except for dependents and
  81.       references */
  82.  
  83.     virtual void CopyContents(TCell* sourceCell);
  84.     // Copy contents of sourceCell 
  85.  
  86.     virtual ~TCell();            // override 
  87.     // Free my lists and then free me 
  88.  
  89.     virtual void EvaluateFormula(Boolean updateDependencies,
  90.                                  Boolean& cellChanged);
  91.     /* Calculate my value by evaluating fFormula. If updateDependencies is true, add me
  92.       to the list of dependents of any cells referenced in my formula. Return true in
  93.       cellChanged if my fValue or fKind has changed */
  94.  
  95.     virtual void GetAsString(CStr255& theString);
  96.     // Return my formula CString 
  97.  
  98.     virtual void GetValueAsString(CStr255& theString);
  99.     // Return a CString representation of my value 
  100.  
  101.     virtual void invalidate();
  102.     // Cause a cell to be marked invalid (in need of re-drawing). 
  103.  
  104.     virtual CPoint GetCoordinates();
  105.     // Returns a CPoint describing the Cell's location on the GridView
  106.  
  107.     virtual Boolean IsEmpty();
  108.     // Return true if I have no formula or value 
  109.  
  110.     virtual void SetDeleteState(Boolean deleted);
  111.     // Mark a cell as deleted or not deleted 
  112.  
  113.     virtual void SetToString(const CStr255& theString);
  114.     // Set my formula to theString and recalculate my value if necessary 
  115.  
  116.     virtual void Recalculate(Boolean forceAutomatic,
  117.                              Boolean updateDependencies);
  118.     // Re-evaluate my formula CString. Notify dependent cells of the change 
  119.  
  120.     virtual void DoUpdate(ChangeID theChange,
  121.                           TObject* changedObject,
  122.                           TObject* changedBy,
  123.                           TDependencySpace* dependencySpace);// Override 
  124.     /* Update this cell in response to a change in a cell referenced in this
  125.       cell's formula */
  126.  
  127.     virtual ArrayIndex GetNoOfDependentCells();
  128.     // Returns the size of the dependent list 
  129.  
  130.     virtual ArrayIndex GetNoOfReferencedCells();
  131.     // Returns the size of the dependent list 
  132.  
  133.     virtual TDependencySpace* GetDependencySpace(); // Override
  134.     // Overridden to return the document's dependency space
  135.  
  136.     virtual void AddDependentCell(TCell* aCell);
  137.     // Add aCell to this cell's list of dependents 
  138.     
  139.     virtual void RemoveDependentCell(TCell* aCell);
  140.     // Remove aCell from this cell's list of dependents 
  141.  
  142.     virtual void ValueToString();
  143.     // Fill fValueString with the CString representation of my value 
  144.  
  145.     virtual short GetDiskSize(Boolean infoRecordOnly);
  146.  
  147.     virtual void ReadFromDisk(TFile* aFile);
  148.  
  149.     virtual void WriteToDisk(TFile* aFile);
  150.  
  151.     virtual void WriteCellCoordinateToDisk(TFile* aFile);
  152.  
  153.     virtual void ReadFromScrap(Handle theScrap, long& scrapOffset);
  154.  
  155.     virtual void WriteToScrap(Handle theScrap, long& scrapOffset);
  156.  
  157. };                                                // TCell 
  158.  
  159.  
  160. //--------------------------------------------------------------------------------------------------
  161. // TCellEditCommand: TCellEditCommand is a command object created to handle the cCopy,
  162. // cCut, and cClear menu commands.
  163. //--------------------------------------------------------------------------------------------------
  164.  
  165. class TCellEditCommand : public TCommand
  166. {
  167.     MA_DECLARE_CLASS;
  168.     
  169. public:
  170.     TCalcDocument*    fCalcDocument;
  171.     RgnHandle        fSelection;                        // cells selected when command was created 
  172.  
  173.     virtual void ICellEditCommand(TCalcDocument* itsDocument,
  174.                                   CommandNumber itsCommand);
  175.     /* Initialize the command object. Save the current cell selection for later
  176.       Undo/Redo */
  177.  
  178.     virtual ~TCellEditCommand();            // override 
  179.     // Clean up our stuff 
  180.  
  181.     virtual void DoIt();            // override 
  182.     // Edit the currently selected cells 
  183.  
  184.     virtual void UndoIt();            // override 
  185.     // Undo the edit of the cells 
  186.  
  187.     virtual void RedoIt();            // override 
  188.     // Redo the edit of the cells 
  189.  
  190.     virtual void Commit();            // override 
  191.     // Commit the edit of the cells 
  192.  
  193.     virtual void CopySelection();
  194.     // Copy the currently selected cell(s) into the clipboard 
  195.  
  196.     virtual void DeleteSelection();
  197.     // Delete the currently selected cell(s) 
  198.  
  199.     virtual void RestoreSelection();
  200.     // Restore the deleted cells 
  201.  
  202.     virtual void ReSelect();
  203.     // Change the selection back to what it was when this command was created 
  204.  
  205. };                                                // TCellEditCommand 
  206.  
  207.  
  208. //--------------------------------------------------------------------------------------------------
  209. // TCellPasteCommand: A command object created to handle the cPaste menu command. 
  210. //--------------------------------------------------------------------------------------------------
  211.  
  212. class TCellPasteCommand : public TCommand
  213. {
  214.     MA_DECLARE_CLASS;
  215.     
  216. public:
  217.     TCalcDocument*    fClipDocument;
  218.     TCalcDocument*    fCalcDocument;
  219.     RgnHandle        fSelection;                    // cells selected when command was created 
  220.     TCellList*        fReplacedCells;                // the cells we pasted over 
  221.  
  222.     virtual void ICellPasteCommand(TCalcDocument* itsDocument);
  223.     // Initialize the command object 
  224.  
  225.     virtual ~TCellPasteCommand();            // override 
  226.     // We don' need this command no more 
  227.  
  228.     virtual void DoIt();            // override 
  229.     // Paste to the currently selected cells. Save this selection for Undo/Redo. 
  230.  
  231.     virtual void UndoIt();            // override 
  232.     // Undo the paste of the cells 
  233.  
  234.     virtual void RedoIt();            // override 
  235.     // Redo the paste of the cells 
  236.  
  237.     virtual void UpdateViews();
  238.     // Redisplay the affected cells 
  239. };                                                // TCellPasteCommand 
  240.  
  241.  
  242. class CCalcCellIterator : public CObjectIterator
  243. {
  244. public:
  245.     CCalcCellIterator(TCalcDocument* theCalcDocument);
  246.  
  247.     CCalcCellIterator(TCellList* theCellList);
  248.     
  249.     virtual ~CCalcCellIterator();
  250.     
  251.     virtual TCell* CurrentCell();
  252.     // returns the current Cell
  253.  
  254.     virtual TCell* FirstCell();
  255.     // return the first Cell in the iteration
  256.  
  257.     virtual TCell* NextCell();
  258.     // advances the iteration and then returns the Cell
  259. };
  260.  
  261.  
  262. class CSelectedExistingCalcCellIterator : public CCalcCellIterator
  263. {
  264. public:
  265.     CSelectedExistingCalcCellIterator(TCalcDocument* theCalcDocument);
  266.  
  267.     virtual ~CSelectedExistingCalcCellIterator();
  268.     
  269.     virtual void Reset();
  270.  
  271. protected:
  272.     RgnHandle fSelectedRegion;
  273.  
  274.     virtual Boolean IsCellSelected(TCell* theCell);
  275.  
  276.     virtual void Advance();
  277. };
  278.  
  279.  
  280. class CExistingCalcCellIterator : public CCalcCellIterator
  281. {
  282. public:
  283.     CExistingCalcCellIterator(TCalcDocument* theCalcDocument);
  284.     
  285.     virtual ~CExistingCalcCellIterator();
  286.     
  287.     virtual void Reset();
  288.  
  289. protected:
  290.     TCalcDocument* fCalcDocument;
  291.  
  292.     virtual Boolean CellExists();
  293.  
  294.     virtual void Advance();
  295. };
  296.  
  297.  
  298. class CReferencedCellIterator : public CNotifierIterator
  299. {
  300. public:
  301.     CReferencedCellIterator(TCell* theCell);
  302.     
  303.     virtual ~CReferencedCellIterator();
  304.     
  305.     virtual void Reset();
  306.  
  307.     virtual TCell* FirstCell();
  308.  
  309.     virtual TCell* CurrentCell();
  310.  
  311.     virtual TCell* NextCell();
  312.  
  313. protected:
  314.     virtual Boolean NotifierIsCell();
  315.  
  316.     virtual void Advance();
  317. };
  318.  
  319.  
  320. class CDependentCellIterator : public CDependentIterator
  321. {
  322. public:
  323.     CDependentCellIterator(TCell* theCell);
  324.     
  325.     virtual ~CDependentCellIterator();
  326.     
  327.     virtual void Reset();
  328.  
  329.     virtual TCell* FirstCell();
  330.  
  331.     virtual TCell* CurrentCell();
  332.  
  333.     virtual TCell* NextCell();
  334.  
  335. protected:
  336.     virtual Boolean DependentIsCell();
  337.  
  338.     virtual void Advance();
  339. };
  340.  
  341.  
  342. class CParseSubscribedText
  343. {
  344. public:
  345.     CParseSubscribedText(TDesignator*    aDesignator,
  346.                          TStream*        aStream,
  347.                          long            length,
  348.                          CRect            subBounds,     // bounds of the subscriber 
  349.                          CRect            pubBounds);    // bounds of the publisher 
  350.  
  351.     virtual CStr255 ParseSubscribedText(Handle    textHdl, GridCell    aGridCell);
  352.  
  353. private:
  354.     CRect            subBounds;                // bounds of the subscriber 
  355.     CRect            pubBounds;                // bounds of the publisher 
  356.     Handle            theText;
  357.     TDesignator*    theDesignator;
  358.     TStream*        theStream;
  359.     long            theLength;
  360.     GridCell        normalizedCell;
  361.     unsigned char*    p;
  362.     long            offset;
  363.     long            where;
  364.     char            aChar;
  365.     long            maxLength;
  366.     
  367.     virtual void ScanTo(char ch);
  368.  
  369.     virtual short NumTabs();
  370. };
  371.  
  372.  
  373. class CCellParser
  374. {                                                // takes the text of a cell and evaluates its contents
  375. public:
  376.     CCellParser(TCell* theCell);
  377.  
  378.     void EvaluateFormula(KindOfCell&    theKind,
  379.                          EvalResult&    theError,
  380.                          ValueType&        theValue,
  381.                          CStr255&        theFormula,
  382.                          Boolean        doUpdateDependencies);
  383.  
  384. protected:
  385.     TCell*            fCell;
  386.     TCalcDocument*    fCalcDocument;
  387.     ValueString        fValueString;
  388.     CStr255            fFormula;
  389.     short            formulaLength;
  390.     short            formulaIndex;
  391.     char            theChar;
  392.     ValueType        theValue;
  393.     Boolean            updateDependencies;
  394.  
  395.     void GetNextChar();
  396.     
  397.     EvalResult DoInteger(short& theInteger);
  398.  
  399.     EvalResult DoNumber(ValueType& theValue);
  400.  
  401.     EvalResult DoCellReference(ValueType& theValue);
  402.  
  403.     EvalResult Term(ValueType& theValue);
  404.  
  405.     EvalResult Factor(ValueType& theValue);
  406.  
  407.     EvalResult Expression(ValueType& theValue);
  408. };
  409.  
  410.  
  411. #endif
  412.  
  413.